from randomizer import get_random_letter
from fatorial import fatorial

get_string = input
def input(*args, **kwargs):
    print(*args, **kwargs)
    return get_string()


def calculate_permutation(list_lenght:int, repetitions:list) -> int:
    '''repetitions: list with the numbers of repetitions in the sequece
    ex:                             a:2, b:2, c:1
       'abacb' :                   [  2,   2,   1]

    calculate permutation with the formule.'''

    result = 1
    for n in repetitions:
        result *= fatorial(n)
    result = fatorial(list_lenght)//result

    return result


def get_permutation_limit(word:str) -> int:
    'calculate permutation of word.'

    rep, history = [], []
    for letter in word:
        total = word.count(letter)
        if (letter not in history) and (total):
            rep.append(total)

            history.append(letter)

    return calculate_permutation(len(word), rep)


def get_permutation(word:str, force_list:bool = False) -> str:
    'returns the possible permutation of a word.'

    history = []
    while len(history) < get_permutation_limit(word):
        new_word = ''
        letter_history = {}

        while len(new_word) < len(word):
            letter = get_random_letter(word)
            if letter in letter_history:
                letter_history[letter] += 1
            else:
                letter_history[letter] = 1
                new_word += letter
                continue

            if letter_history[letter] <= word.count(letter):
                new_word += letter

        if new_word not in history:
            history.append(new_word)
            if not force_list:
                yield new_word


def get_range_limit(sequence:list, lenght:int) -> int:
    return len(sequence)**lenght


def range_into_sequence(chars:list, lenght:int) -> str:
    sequence = [chars[0] for i in range(lenght)]

    history = []
    limit =  get_range_limit(chars, lenght)
    while len(history) < limit:

        if sequence not in history:
            history.append(list(sequence))
            yield sequence

        for char in range(len(sequence)-1,-1,-1):
            next_char = 0
            char_quered = chars.index(sequence[char])
            if char_quered < len(chars)-1:
                next_char = char_quered+1

            if sequence[char+1] == chars[-1]:
                sequence[char] = chars[next_char]
            else:
                sequence[char+1] = chars[next_char]


if __name__ == "__main__":
    from sys import argv as args
    for p in range_into_sequence(list(args[1]), int(args[2])):
        print(''.join(p))
